<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail" :labelAlign="'left'">
        <a-row>
          <a-col :span="8">
            <a-form-model-item label="计划名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入计划名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item ref="datePickerRef" label="计划年度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="jihuaniandu">
              <!-- <j-date placeholder="请选择保养日期" v-model="model.riqi" style="width: 100%" mode="year"  :value="state.time" :open="state.isopen" @focus="changeopen" @panelChange="handlePanelChange1"/> -->
<!--              todo 输入框输入不管用-->
              <a-date-picker :disabledDate="disabledDate"  inputReadOnly allowClear v-model="model.jihuaniandu" mode="year" format="YYYY"  :open="isopen" placeholder="请选择计划年度" @change="clearValue"
                 @focus="changeopen" @panelChange="handlePanelChange1" style="width: 100%;">
              </a-date-picker>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="保养层级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cengji">
              <j-dict-select-tag type="list" v-model="model.cengji" dictCode="baoyangcengji" placeholder="请选择保养层级" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="保养方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fangshi">
              <j-dict-select-tag type="list" v-model="model.fangshi" dictCode="baoyangfangshi" placeholder="请选择保养方式" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="保养日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="riqi">
              <j-date placeholder="请选择保养日期" v-model="model.riqi" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="过期天数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="guoqitianshu">
              <a-input-number v-model="model.guoqitianshu" placeholder="请输入过期天数" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="间隔时长" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="jiangeshichang">
              <a-input-number v-model="model.jiangeshichang" placeholder="请输入间隔时长" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="负责人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fuzeren">
              <!--              <a-input v-model="model.fuzeren" placeholder="请输入负责人"  ></a-input>-->
              <j-select-user-by-dep v-model="model.fuzeren" :multi="true" placeholder="请输入负责人" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="beizhu">
              <a-input v-model="model.beizhu" rows="4" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="年度保养计划" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="plan">
              <j-dict-select-tag type="list" v-model="model.plan" dictCode="plan" placeholder="请选择年度保养计划" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="保养设备" :labelCol="{ xs: { span: 24 }, sm: { span: 24 } }"
              :wrapperCol="{ xs: { span: 24 }, sm: { span: 24 } }" prop="shebei">
              <j-popup v-model="model.shebei_dictText" field="shebei" org-fields="device_name,device_id"
                dest-fields="shebei_dictText,shebei" code="ene_device" :multi="true" @input="popupCallback" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="保养项目" :labelCol="{ xs: { span: 24 }, sm: { span: 24 } }"
              :wrapperCol="{ xs: { span: 24 }, sm: { span: 24 } }" prop="xiangmu">
              <j-popup v-model="model.xiangmu_dictText" field="xiangmu" org-fields="name,id"
                dest-fields="xiangmu_dictText,xiangmu" code="gd_baoyang_xiangmu" :multi="true" @input="popupCallback" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import dayjs from 'dayjs';

export default {
  name: 'GdBaoyangJihuaForm',
  components: {
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 23 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 23 },
      },
      confirmLoading: false,
      validatorRules: {
        name: [
          { required: true, message: '请输入计划名称!' },
        ],
        jihuaniandu: [
          { required: true, message: '请输入计划年度!',trigger: 'change' },
        ],
        cengji: [
          { required: true, message: '请输入保养层级!' },
        ],
        fangshi: [
          { required: true, message: '请输入保养方式!' },
        ],
        riqi: [
          { required: true, message: '请输入保养日期!' },
        ],
        guoqitianshu: [
          { required: true, message: '请输入过期天数!' },
        ],
        jiangeshichang: [
          { required: true, message: '请输入间隔时长!' },
        ],
        shebei: [
          { required: true, message: '请输入保养设备!' },
        ],
      },
      url: {
        add: "/gongdan/gdBaoyangJihua/add",
        edit: "/gongdan/gdBaoyangJihua/edit",
        queryById: "/gongdan/gdBaoyangJihua/queryById"
      },
      isopen:false,

    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {

    disabledDate (current) {
      const date=new Date()
      return current && current < dayjs(date).year(dayjs(date).year()).startOf("year")
    },
    clearValue(date,dateString){

      this.$set(this.model,'jihuaniandu',null)
      console.log("after",this.model)


    },
    handlePanelChange1(value, mode) {
      console.log(value)

      this.$set(this.model,'jihuaniandu',dayjs(value).format("YYYY"))
      this.isopen = false
      this.$refs.datePickerRef.clearValidate();

    },
    changeopen() {
      this.isopen = true;
    },
    add() {
      this.edit(this.modelDefault);
    },
    edit(record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
    popupCallback(value, row) {
      this.model = Object.assign(this.model, row);
    },
  }
}
</script>
<style scoped>
/* :global {
  .ant-form-item {
    margin-bottom: 2px !important;
  }
} */
</style>